mini.parse();

function GoodsGroupHandler() {
};

GoodsGroupHandler.instance = new GoodsGroupHandler();

GoodsGroupHandler.isCollapseOrExpand = true;
GoodsGroupHandler.isQueryBtnExpended = false;
GoodsGroupHandler.loadUrl = "/getGoodsGroupTreeGrid.do";

//获得全局的商品分类数据表格对象
GoodsGroupHandler.goodsGroupTreeGrid = mini.get("goodsGroupTreeGrid");

//加载树形表格数据
GoodsGroupHandler.loadTree = function(){
	GoodsGroupHandler.goodsGroupTreeGrid.load(__root + GoodsGroupHandler.loadUrl);
}

/**
 * 根据用户权限展示功能按钮
 */
GoodsGroupHandler.loadToolbar = function() {
	var array = new Array("goods_group_add", "goods_group_edit", "goods_group_del", "goods_group_expandAll","goods_group_query","goods_group_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
GoodsGroupHandler.loadToolbar();

/**
 * 添加或编辑之后重新收缩
 */
GoodsGroupHandler.collapseOnLoad = function(){
	mini.get("goods_group_expandAll").setText("展开所有");
	mini.get("goods_group_expandAll").setIconCls("icon-btn-expand");
	GoodsGroupHandler.isCollapseOrExpand = true;
}

/**
 * 弹出添加商品分类窗口
 */
GoodsGroupHandler.addGoodsGroup = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(381, 376, "添加商品分类");  
	miniWin.setOption("bodyStyle","padding:0px;");
	
	var rowData = GoodsGroupHandler.goodsGroupTreeGrid.getSelectedNode();
	var selectedId = null;
	var selectedName = null;
	
	if(rowData != null){
		selectedId = rowData.bggId;
		selectedName = rowData.bggName;
	}

	miniWin.load("goodsGroupForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new",bggId: selectedId, bggName: selectedName};
        iframe.contentWindow.GoodsGroupFormHandler.passData(data);
	},function(action){
		GoodsGroupHandler.loadTree();
		GoodsGroupHandler.collapseOnLoad();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑商品分类窗口
 */
GoodsGroupHandler.editGoodsGroup = function() {
	var rowData = GoodsGroupHandler.goodsGroupTreeGrid.getSelectedNode();
	if(rowData){
		miniWin.afreshNewWin();
		miniWin.setParams(381, 376, "编辑商品分类[<font color='blue'>" + rowData.bggName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("goodsGroupForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bggId: rowData.bggId};
            iframe.contentWindow.GoodsGroupFormHandler.passData(data);
		},function(action){
			GoodsGroupHandler.loadTree();
			GoodsGroupHandler.collapseOnLoad();
		});
		
		miniWin.show();
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 懒加载treegrid加载驱动
 * @param {} e
 */
GoodsGroupHandler.onBeforeTreeLoad = function(e) {
	var tree = e.sender;    //树控件
    var node = e.node;      //当前节点
    var params = e.params;  //参数对象
}

/**
 * 更新商品分类状态
 */
GoodsGroupHandler.updateBggState = function() {
    if (!MiniuiTools.hasPurview("goods_group_edit")) {
		return;
	}
	
	//loading()、mask()、GoodsGroupHandler.goodsGroupTreeGrid.loadingMsg = "加载中...";
	var rowData = GoodsGroupHandler.goodsGroupTreeGrid.getSelectedNode();
	var bggId = rowData.bggId;
	GoodsGroupHandler.goodsGroupTreeGrid.loading();
	
    $.ajax({
        url: __root + "/updateBggStateById.do",
        data: {
        	bggId: bggId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
        	var temp;
        	if(rowData.bggState == 0) temp = 1;
        	else temp = 0;
        	
        	GoodsGroupHandler.goodsGroupTreeGrid.updateNode(rowData, {bggState: temp});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	GoodsGroupHandler.goodsGroupTreeGrid.unmask();
        }
    });	
};

/**
 * 更新是否精品推荐
 */
GoodsGroupHandler.updateBggBest = function() {
    if (!MiniuiTools.hasPurview("goods_group_edit")) {
		return;
	}
	
	var rowData = GoodsGroupHandler.goodsGroupTreeGrid.getSelectedNode();
	var bggId = rowData.bggId;	
	GoodsGroupHandler.goodsGroupTreeGrid.loading();
	
    $.ajax({
        url: __root + "/updateBggBestById.do",
        data: {
        	bggId: bggId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            var temp;
        	if(rowData.bggBest == 0) temp = 1;
        	else temp = 0;
        	
        	GoodsGroupHandler.goodsGroupTreeGrid.updateNode(rowData, {bggBest: temp});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	GoodsGroupHandler.goodsGroupTreeGrid.unmask();
        }
    });	
};

/**
 * 更新是否新品推荐
 */
GoodsGroupHandler.updateBggNew = function() {
    if (!MiniuiTools.hasPurview("goods_group_edit")) {
		return;
	}
	
	var rowData = GoodsGroupHandler.goodsGroupTreeGrid.getSelectedNode();
	var bggId = rowData.bggId;	
	GoodsGroupHandler.goodsGroupTreeGrid.loading();
	
    $.ajax({
        url: __root + "/updateBggNewById.do",
        data: {
        	bggId: bggId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            var temp;
        	if(rowData.bggNew == 0) temp = 1;
        	else temp = 0;
        	
        	GoodsGroupHandler.goodsGroupTreeGrid.updateNode(rowData, {bggNew: temp});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	GoodsGroupHandler.goodsGroupTreeGrid.unmask();
        }
    });	
};

/**
 * 更新是否热销推荐
 */
GoodsGroupHandler.updateBggHot = function() {
    if (!MiniuiTools.hasPurview("goods_group_edit")) {
		return;
	}
	
	var rowData = GoodsGroupHandler.goodsGroupTreeGrid.getSelectedNode();
	var bggId = rowData.bggId;	
	GoodsGroupHandler.goodsGroupTreeGrid.loading();
	
    $.ajax({
        url: __root + "/updateBggHotById.do",
        data: {
        	bggId: bggId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            var temp;
        	if(rowData.bggHot == 0) temp = 1;
        	else temp = 0;
        	
        	GoodsGroupHandler.goodsGroupTreeGrid.updateNode(rowData, {bggHot: temp});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	GoodsGroupHandler.goodsGroupTreeGrid.unmask();
        }
    });	
};

/**
 * 更新是否团购推荐
 */
GoodsGroupHandler.updateBggBuy = function() {
    if (!MiniuiTools.hasPurview("goods_group_edit")) {
		return;
	}
	
	var rowData = GoodsGroupHandler.goodsGroupTreeGrid.getSelectedNode();
	var bggId = rowData.bggId;	
	GoodsGroupHandler.goodsGroupTreeGrid.loading();
	
    $.ajax({
        url: __root + "/updateBggBuyById.do",
        data: {
        	bggId: bggId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            var temp;
        	if(rowData.bggBuy == 0) temp = 1;
        	else temp = 0;
        	
        	GoodsGroupHandler.goodsGroupTreeGrid.updateNode(rowData, {bggBuy: temp});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	GoodsGroupHandler.goodsGroupTreeGrid.unmask();
        }
    });	
};

/**
 * 删除配送区域资源
 */
GoodsGroupHandler.delGoodsGroup = function() {
	var rowData = GoodsGroupHandler.goodsGroupTreeGrid.getSelectedNode();

	if(rowData){
		mini.confirm("确认删除该商品分类及其下节点？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					// 判断当前分类或其子分类是否被使用
					if(GoodsGroupHandler.getBgiByBgg(rowData.bggId)){
						MiniuiTools.miniWarning("系统提示", "选择的记录或子记录存在商品信息关联，请检查后重试！");
						return;	
					}
					
                    $.ajax({
                        url: __root + "/delBggById.do",
                        data: {
				        	bggId: rowData.bggId,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		GoodsGroupHandler.goodsGroupTreeGrid.removeNode(rowData);
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 检查该分类及其子分类下面是否有相应的商品，如果有商品，则不能立即删除
 * @param {} bggId
 * @return {}
 */
GoodsGroupHandler.getBgiByBgg = function(bggId){
	var isUsed = false;
	
	$.ajax({
        url: __root + "/getBgiByBgg.do?bggId=" +bggId,
        dataType: 'json',
        async: false,   // 同步执行，默认为true
        success: function (text) {
        	if(text == "1"){
        		isUsed = true;
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        }
    });
    
    return isUsed;
}

/**
 * 展开或折叠查询商品分类面板
 */
GoodsGroupHandler.queryGoodsGroup = function() {
	if (GoodsGroupHandler.isQueryBtnExpended) {
		GoodsGroupHandler.isQueryBtnExpended = false;
		mini.get("goods_group_query").setText("展开查询");
		mini.get("goods_group_query").setIconCls("icon-btn-expand");
	} else {
		GoodsGroupHandler.isQueryBtnExpended = true;
		mini.get("goods_group_query").setText("折叠查询");
		mini.get("goods_group_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryGoodsGroupDiv").toggle(500);
    setTimeout(function(){mini.get("goodsGroupPanel").doLayout();},600);
};

/**
 * 执行查询商品分类的方法
 */
GoodsGroupHandler.doQueryGoodsGroup = function(){
	var bggName = mini.get("query_bggName").getValue();
	
	if (bggName == "") {
         GoodsGroupHandler.goodsGroupTreeGrid.clearFilter();
    } else {
		GoodsGroupHandler.goodsGroupTreeGrid.filter(function (node) {
			var text = node.bggName;
			
		    if (text.indexOf(bggName) != -1) {
		        return true;
		    }
		});
    }
};

/**
 * 展开/折叠所有节点
 */
GoodsGroupHandler.expandOrCollapseAll = function(){
	if(GoodsGroupHandler.isCollapseOrExpand){
		GoodsGroupHandler.goodsGroupTreeGrid.expandAll();
		mini.get("goods_group_expandAll").setText("折叠所有");
		mini.get("goods_group_expandAll").setIconCls("icon-btn-collapse");
		GoodsGroupHandler.isCollapseOrExpand = false;
	}else{
		GoodsGroupHandler.goodsGroupTreeGrid.collapseAll();
		GoodsGroupHandler.collapseOnLoad();
	}
}

/**
 * 刷新页面数据及布局
 */
GoodsGroupHandler.afresh = function(){
	//GoodsGroupHandler.doQueryGoodsGroup();
	GoodsGroupHandler.loadTree();
	mini.get("goodsGroupPanel").doLayout();
}

/**
 * 转移商品至其它商品分类
 */
GoodsGroupHandler.moveGoodsById = function(){
	if (!MiniuiTools.hasPurview("goods_group_edit")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	 var msgId = mini.loading("功能实现中，敬请期待 ...", "系统提示");
 	 setTimeout(function () {mini.hideMessageBox(msgId);}, 2000);
     return;
     
	miniWin.afreshNewWin();
	miniWin.setParams(520, 200, "转移商品分类");

	var selected = GoodsGroupHandler.goodsGroupTreeGrid.getSelectedNode();
	miniWin.load("transferGoods.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {rowData: selected};
        iframe.contentWindow.TransferGoodsHandler.passData(data);
	},function(action){
		GoodsGroupHandler.loadTree();
	});
	
	miniWin.show();
}

/**
 * DrawCell 绘制单元格，对于树形表格的渲染，需将渲染置于数据加载的前面。即先“drawcell”然后再loadTree
 */
GoodsGroupHandler.goodsGroupTreeGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "bggBest") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggBest()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggBest()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
    if (column.field == "bggNew") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggNew()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggNew()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
    if (column.field == "bggHot") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggHot()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggHot()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
    if (column.field == "bggBuy") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggBuy()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggBuy()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
     if (column.field == "bggState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsGroupHandler.updateBggState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
    if(column.field == "bggOperation"){
    	e.cellHtml = "<span><a style='cursor: pointer;' onclick='GoodsGroupHandler.moveGoodsById()'>转移商品</a></span>";
    }
});

/**
 * 加载树形表格
 */
GoodsGroupHandler.loadTree();
